package orlowski.kamil.simple.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.test.ActivityUnitTestCase;
import android.widget.TextView;
import orlowski.kamil.simple.app.MainApplication;
import orlowski.kamil.simple.app.R;
import orlowski.kamil.simple.app.domain.Country;

/**
 * Created by Kamil on 2014-10-20.
 * Detail Activity tests
 */
public class DetailsActivityTests extends ActivityUnitTestCase<DetailsActivity> {
    private Country selectedCountry;
    private MainApplication mainApplication;

    public DetailsActivityTests() {
        super(DetailsActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        this.selectedCountry = new Country("pl", "Polska", R.drawable.pl, 1);
        mainApplication = new MainApplication();
        mainApplication.setSelectedCountry(selectedCountry);
        setApplication(mainApplication);
    }

    public void testPreConditions() {
        startActivity(new Intent(getInstrumentation().getTargetContext(), DetailsActivity.class), null, null);

        Activity activity = getActivity();
        assertNotNull(activity.findViewById(R.id.CountryCode));
        assertNotNull(activity.findViewById(R.id.CountryName));
        assertNotNull(activity.findViewById(R.id.CountryImage));
    }

    public void testThatAllFieldsAreSetCorrectly() {
        startActivity(new Intent(getInstrumentation().getTargetContext(), DetailsActivity.class), null, null);
        assertEquals(selectedCountry.getCountryName(), getViewText(R.id.CountryName));
        assertEquals(selectedCountry.getCountryCode(), getViewText(R.id.CountryCode));
    }

    private String getViewText(int textViewId) {
        return ((TextView) getActivity().findViewById(textViewId)).getText().toString();
    }
}
